<?php

namespace app\aiapi\controller;

/*
 * 百度Ai接口文字识别
 */

use think\facade\Request;

class BaiAiCard
{
    protected $access_token;
    public function __construct()
    {
        $url = 'https://aip.baidubce.com/oauth/2.0/token';
        $post_data['grant_type']       = 'client_credentials';
        $post_data['client_id']      = 'KpdHvx1XGZ2GaNgIebWdi2Iz';
        $post_data['client_secret'] = 'KQgGTiyipgFiirc50Gju4Abmf7McrtzP';
        $o = "";
        foreach ( $post_data as $k => $v )
        {
            $o.= "$k=" . urlencode( $v ). "&" ;
        }
        $post_data = substr($o,0,-1);

        $res = http_post($url, $post_data);
        $res = json_decode($res);
        $this -> access_token = $res -> access_token;
    }


    /**
     * @api {get}  aiapi/BaiAiCard/vehicleIdCard 身份证识别
     * @apiVersion 0.1.0
     * @apiName vehicleIdCard
     * @apiGroup 用户中心
     *
     * @apiParam {string} image: 图片支付 类型type:file.
     * @apiParam {string} id_card_side: front：身份证含照片的一面,back：身份证带国徽的一面.
     *
     * @apiSuccess {String} admin_admin_id 用户id.
     * @apiSuccess {String} admin_token  用户token.
     * @apiSampleRequest aiapi/BaiAiCard/vehicleIdCard
     * @apiSuccessExample Success-Response:
     *     HTTP/1.1 200 OK
     *     {
     *           code: 200,
     *           msg: "登录成功",
     *           data: {
     *           admin_admin_id: 2,
     *           admin_token: "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJsampBZG1pbiIsImlhdCI6MTYyOTE2Nzg2OCwibmJmIjoxNjI5MTY3ODY4LCJleHAiOjE2MjkyMTEwNjgsImRhdGEiOnsiYWRtaW5fYWRtaW5faWQiOjIsImxvZ2luX3RpbWUiOiIyMDIxLTA4LTE3IDEwOjM3OjQ4IiwibG9naW5faXAiOiIxMjcuMC4wLjEifX0.QhnW5Y-3QiQ1tJBkpIHkhIcBr6JplHaI_byvZxN9k8E"
     *               }
     *           }
     *
     * @apiError UserNotFound The id of the User was not found.
     *
     * @apiErrorExample Error-Response:
     *     HTTP/1.1 404 Not Found
     *     {
     *       "error": "UserNotFound"
     *     }
     */


    public function vehicleIdCard()
    {
        $id_card_side = Request::instance() -> post('id_card_side');
        $img = $_FILES["image"];
        $imgName = explode(".",$img["name"]);
        $imgArray = ['jpg','bmp','png'];
        if (!in_array($imgName[1],$imgArray)){
            return error("图片格式不支持");
        }

         $url = 'https://aip.baidubce.com/rest/2.0/ocr/v1/idcard?access_token=' . $this -> access_token;
         $img = base64EncodeImage($img["tmp_name"]);
         $img = str_replace(PHP_EOL, '', $img);
        //$img = CardService::processImage($img["tmp_name"]);
        $bodys = array(
            'id_card_side' => $id_card_side,
            'image' => $img
        );
        $header["Content-Type"] = "application/x-www-form-urlencoded";
        $res = http_post($url, $header,$bodys);
        $res = json_decode($res);
        return success($res);
    }


    /**
     * @api {get}  aiapi/BaiAiCard/businessLicense 营业执照
     * @apiVersion 0.1.0
     * @apiName businessLicense
     * @apiGroup 用户中心
     *
     * @apiParam {string} image: 图片支付 类型type:file.
     *
     * @apiSuccess {String} admin_admin_id 用户id.
     * @apiSuccess {String} admin_token  用户token.
     * @apiSampleRequest aiapi/BaiAiCard/businessLicense
     * @apiSuccessExample Success-Response:
     *     HTTP/1.1 200 OK
     *     {
     *           code: 200,
     *           msg: "登录成功",
     *           data: {
     *           admin_admin_id: 2,
     *           admin_token: "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJsampBZG1pbiIsImlhdCI6MTYyOTE2Nzg2OCwibmJmIjoxNjI5MTY3ODY4LCJleHAiOjE2MjkyMTEwNjgsImRhdGEiOnsiYWRtaW5fYWRtaW5faWQiOjIsImxvZ2luX3RpbWUiOiIyMDIxLTA4LTE3IDEwOjM3OjQ4IiwibG9naW5faXAiOiIxMjcuMC4wLjEifX0.QhnW5Y-3QiQ1tJBkpIHkhIcBr6JplHaI_byvZxN9k8E"
     *               }
     *           }
     *
     * @apiError UserNotFound The id of the User was not found.
     *
     * @apiErrorExample Error-Response:
     *     HTTP/1.1 404 Not Found
     *     {
     *       "error": "UserNotFound"
     *     }
     */

    public function businessLicense()
    {

        $img = $_FILES["image"];
        $imgName = explode(".",$img["name"]);
        $imgArray = ['jpg','bmp','png'];
        if (!in_array($imgName[1],$imgArray)){
            return error("图片格式不支持");
        }

        $url = 'https://aip.baidubce.com/rest/2.0/ocr/v1/business_license?access_token=' . $this -> access_token;
        $img = base64EncodeImage($img["tmp_name"]);
        $img = str_replace(PHP_EOL, '', $img);

        //$img = CardService::processImage($img["tmp_name"]);
        $bodys = array(
            'image' => $img
        );
        $header["Content-Type"] = "application/x-www-form-urlencoded";
        $res = http_post($url, $header,$bodys);
        $res = json_decode($res);
        return success($res);
    }


    /**
     * @api {get}  aiapi/BaiAiCard/bankCard 营业执照
     * @apiVersion 0.1.0
     * @apiName bankCard
     * @apiGroup 用户中心
     *
     * @apiParam {string} image: 图片支付 类型type:file.
     *
     * @apiSuccess {String} admin_admin_id 用户id.
     * @apiSuccess {String} admin_token  用户token.
     * @apiSampleRequest aiapi/BaiAiCard/bankCard
     * @apiSuccessExample Success-Response:
     *     HTTP/1.1 200 OK
     *     {
     *           code: 200,
     *           msg: "登录成功",
     *           data: {
     *           admin_admin_id: 2,
     *           admin_token: "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJsampBZG1pbiIsImlhdCI6MTYyOTE2Nzg2OCwibmJmIjoxNjI5MTY3ODY4LCJleHAiOjE2MjkyMTEwNjgsImRhdGEiOnsiYWRtaW5fYWRtaW5faWQiOjIsImxvZ2luX3RpbWUiOiIyMDIxLTA4LTE3IDEwOjM3OjQ4IiwibG9naW5faXAiOiIxMjcuMC4wLjEifX0.QhnW5Y-3QiQ1tJBkpIHkhIcBr6JplHaI_byvZxN9k8E"
     *               }
     *           }
     *
     * @apiError UserNotFound The id of the User was not found.
     *
     * @apiErrorExample Error-Response:
     *     HTTP/1.1 404 Not Found
     *     {
     *       "error": "UserNotFound"
     *     }
     */
    public function bankCard()
    {
        $img = $_FILES["image"];
        $imgName = explode(".",$img["name"]);
        $imgArray = ['jpg','bmp','png'];
        if (!in_array($imgName[1],$imgArray)){
            return error("图片格式不支持");
        }

        $url = 'https://aip.baidubce.com/rest/2.0/ocr/v1/bankcard?access_token=' . $this -> access_token;
         $img = base64EncodeImage($img["tmp_name"]);
         $img = str_replace(PHP_EOL, '', $img);
        //$img = CardService::processImage($img["tmp_name"]);
        $bodys = array(
            'image' => $img
        );
        $header["Content-Type"] = "application/x-www-form-urlencoded";
        $res = http_post($url, $header,$bodys);
        $res = json_decode($res);
        return success($res);
    }
}